<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>发文管理</title>
    <link rel="stylesheet" href="${request.contextPath}/layuiadmin/layui/css/layui.css" media="all">
</head>
<style>
    .layui-layer-page .layui-layer-content {
        overflow: visible !important;
    }
</style>
<body style="background-color: #F2F2F2;" >
<div style="padding: 20px; ">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <form class="layui-form" method="post" style="margin-top: 10px" lay-filter="dataForm">
                        <input type="text" name="id"  autocomplete="off" class="layui-input" value="${(item.id)!}" style="display: none">

                        <div class="layui-form-item">
                            <label class="layui-form-label">姓名</label>
                            <div class="layui-input-block">
                                <input type="text" name="name" placeholder="" lay-verify="required" autocomplete="off" class="layui-input"   value="${(item.name)!}">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">用户名</label>
                            <div class="layui-input-block">
                                <input type="text" id="username" name="username" placeholder="" lay-verify="required" autocomplete="off" class="layui-input" lay-filter="username" value="${(item.username)!}">
                            </div>
                        </div>


                        <div class="layui-form-item">
                            <label class="layui-form-label">密码</label>
                            <div class="layui-input-block">
                                <input type="text" name="password" placeholder="" lay-verify="required" autocomplete="off" class="layui-input" value="${(item.password)!}">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">电话</label>
                            <div class="layui-input-block">
                                <input type="text" name="phone" placeholder="" autocomplete="off" class="layui-input" value="${(item.phone)!}">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">站点</label>
                            <div class="layui-input-block">
                                <select id="siteid" name="siteid" lay-verify="required" value="${(item.siteid)!}">
                                </select>
                            </div>
                        </div>

                        <button id="add" class="layui-btn" lay-filter="submit" lay-submit style="display: none">提交</button>
                    </form>

                </div>
            </div>
        </div>
    </div>
</div>
<script src="${request.contextPath}/layuiadmin/layui/layui.js"></script>

<script>
    layui.config({
        base: '${request.contextPath}/layuiadmin/modules/' //静态资源所在路径
    }).extend({
        xmSelect: 'xm-select'
    }).use(['form','jquery','laydate','xmSelect'], function(){
       var form=layui.form;
       var $=layui.$;
        var laydate = layui.laydate;
        var xmSelect = layui.xmSelect;

        //获取所有站点
        $.ajax({
            type:"post",
            url: "${request.contextPath}/site/getAll?type=null",
            contentType:'application/json; charset=UTF-8',
            // data: JSON.stringify(data.field),
            // data: {customer:data.field},
            // data:data.field,
            async:false,
            success: function(res) {
                // console.log(res)
                if(res.code===200){
                    var siteidList=res.data;
                    var siteidOption="<option value=''></option>";
                    for(var i=0;i<siteidList.length;i++){
                        var siteid=siteidList[i];
                        siteidOption=siteidOption+"<option value='"+siteid.id+"'>"+siteid.name+"</option>"
                    }
                    siteidOption=siteidOption+"<option value='${(item.siteid)!}'>${(item.siteid)!}</option>"
                    console.log(siteidOption)
                    $("#siteid").html(siteidOption);

                    //回显站点
                    form.val('dataForm', {
                        "siteid": "${(item.siteid)!}" // "name": "value"
                    });

                    form.render()
                }
            },
            error: function(res) {
                layer.alert(res.msg,{icon:2})
            }
        })






        $("#username").blur(function () {
           var username= $("#username").val()

           $.ajax({
                type:"get",
                url: "${request.contextPath}/${page}/checkUserName?username="+username,
                contentType:'application/json; charset=UTF-8',
                success: function(res) {
                    console.log("=====success=====")
                    console.log(res)
                    if(res.code===200){

                    }else {
                        parent.layer.alert("用户名已存在，请更换一个用户名")
                        $("#username").val("")
                    }
                },
                error: function(res) {
                    layer.alert(res.msg,{icon:2})
                }
            })
        })

        form.on('submit(submit)', function(data){
            console.log(data.field);
            $.ajax({
                type:"post",
                url: "${request.contextPath}/user/saveItem",
                contentType:'application/json; charset=UTF-8',
                data: JSON.stringify(data.field),
                // data: {customer:data.field},
                // data:data.field,
                success: function(res) {
                    console.log("=====success=====")
                    console.log(res)
                    if(res.code===200){
                        layer.alert(res.msg,{icon:1},function () {
                            //调用父页面的关闭方法。
                            parent.closeAddLayer();
                            parent.reloadCustomerList();
                        })
                    }
                },
                error: function(res) {
                    layer.alert(res.msg,{icon:2})
                }
            })
            return false;
        });
        window.closeLayer=function(index){
            layer.close(index)
        }


    });
</script>

</body>
</html>